/**
 * Created by zheng on 2017/12/24.
 */


function calAngle(p1, p2) {
  return Math.atan2(p2[1] - p1[1], p2[0] - p1[0]);
};

function calDistance(p1, p2) {
  return Math.sqrt(Math.pow(p1[0] - p2[0], 2) + Math.pow(p1[1] - p2[1], 2));
};

exports.calAngle = calAngle;
exports.calDistance = calDistance;
exports.animationFunction = {
  bezier:(starty, endy, t)=>{
    return (1.-t)*(1.-t)*starty+2.*t*(1.-t)*   (endy/2+starty/2)+t*t*endy;
  }
};


exports.Galaxy = function Galaxy(x,y){

  this.x = x;
  this.y = y;
  this.stars = [];
  this.dust = [];
  this.drag = Math.random();
  this.angleOffsetX = 2.* Math.PI * Math.random();
  this.angleOffsetY = 2. * Math.PI * Math.random();
  this.realAngleOffsetX = 0;
  this.realAngleOffsetY = 0;

  this.color = {
    r: Math.round(50 + Math.random() * 100),
    g: Math.round(50 + Math.random() * 100),
    b: Math.round(150 + Math.random() * 100)
  }

  this.calStarParams=(star)=>{
    star.angle = calAngle([this.x,this.y],[star.x,star.y]);   //计算与中心点的角度
    star.distance = calDistance([this.x,this.y],[star.x,star.y]); //计算与中心点的距离
  }

  this.calculateCenter = ()=>{
    if (!this.stars.length) return;
    this.x = this.stars.map((s)=> {   //计算各个星星的横坐标的平均值
      return s.x;
    }).reduce((previous, current) =>{
      return previous + current;
    }) / this.stars.length;

    this.y = this.stars.map((s)=> {     //计算各个星星的纵坐标的平均值
      return s.y;
    }).reduce((previous, current)=> {
      return previous + current;
    }) / this.stars.length;

    let g = this;
    this.stars.forEach(g.calStarParams);   //更新星星的参数
  }



  this.getRandomStar= ()=>{     //获取一颗随机位置大小的星星
    let star = {};
    let vh = window.innerHeight;
    let vw = window.innerWidth;
    star.x = vw* Math.random(vw);
    star.y = vh* Math.random(vh);
    star.radius =  Math.random() + 0.25;          //星星的大小
    star.speed = Math.random();
    return star;
  }
  console.log('this',this);
  return this;

}




